package com.mgq.netty.testEventLoop;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

public class NettyServer {

    public static void main(String[] args) {
        //1.启动器,负责组装netty组件,启动服务器
        new ServerBootstrap()
                //2.BossEventLoop,WorkEventLoop(selector,thread) group组
                .group(new NioEventLoopGroup())
                //3.选择服务器的ServerSocketChannel实现
                .channel(NioServerSocketChannel.class)
                //4.boss负责处理连接,work负责处理读写,决定了work能够执行那些操作(handler)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    @Override //在连接建立后被调用
                    protected void initChannel(NioSocketChannel ch) throws Exception {
                        //添加具体的handler
                       /* ch.pipeline().addLast(new StringDecoder());//将ByteBuf 转为字符串
                        ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {//自定义handler
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                                System.out.println(msg);
                            }
                        });*/
                    }
                })
                //绑定端口
                .bind(8080);

}
}
